// pages/managePage/managePage.js
var util = require('../../utils/util');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow:true,
    show: false,
    navList:[
        {
            src:'/resources/upload.png',
            text:'上传菜单',
            link:'/pages/upMenuList/upMenuList',
            warning:false
        },
        {
            src:'/resources/record.png',
            text:'订餐历史',
            link:'/pages/record/record',
            warning:false
        },
    ],
     rice_type: 1,
     type: 1,
     uname: '',
     user_id: '',
     content_id: '',
     uerList: [],
     rice_one: 0,
     rice_two: 0,
     countList:[
         // 1
        {
            text:'常规餐',
            count:'0',
            value: 1
        },
        // 3
        {
            text:'全素餐',
            count:'0',
            value: 3
        },
        // 2
        {
            text:'清真餐',
            count:'0',
            value: 2
        }
      ],
      department: [
          {
              id: '',
              content_name: '请选择部门'
          }
      ],
      dIndex: 0,
      currentNav:0,
      amOpm: '午餐'
  },
  bindDepartmentChange(e) {
    console.log(e)
    let dIndex = e.detail.value;
    let department_id = this.data.department[dIndex].id;
    this.setData({
      content_id: department_id,
      dIndex: dIndex
    })
    this.getOrderList()
  },
  // 获取列表
  getOrderList() {
      const that = this;
      const data = {
          date: that.data.date,
          type: that.data.type,
          rice_type: that.data.rice_type,
          user_id: that.data.user_id,
          content_id: that.data.content_id,
          date_type: that.data.dateType
      }
      util.getAjax(`${app.globalData.apiUrl}/Api/Food/get_user_get_food_list`,res => {
        const countList = that.data.countList;
        countList[0].count = res.data.type_one;
        countList[1].count = res.data.type_three;
        countList[2].count = res.data.type_two;
        let rice_one = that.data.rice_one;
        let rice_two = that.data.rice_two;
        if(data.type == 1) {
          rice_one = res.data.type_one_one;
          rice_two = res.data.type_one_two;
        } else if(data.type == 2) {
          rice_one = res.data.type_two_one;
          rice_two = res.data.type_two_two;
        } else if(data.type == 3) {
          rice_one = res.data.type_three_one;
          rice_two = res.data.type_three_two;
        }
        that.setData({
            countList: countList,
            rice_one: rice_one,
            rice_two: rice_two
        })   
      }, data)
      util.getAjax(`${app.globalData.apiUrl}/Api/Food/get_user_food_list`, res => {
        console.log('list', res)
        that.setData({
          userList: res.data.result
        })
      }, data)
  },
  // 是否要主食
  radioChange(e) {
     let type = e.currentTarget.dataset.type;
     this.setData({
         rice_type: type
     })
     this.getOrderList()
  },
  radioDatetypeChange(e) {
    let type = e.currentTarget.dataset.type;
    this.setData({
        dateType: type
    })
    this.getOrderList()
  },
  // 获取部门
  getDepartment() {
      let that = this;
      util.getAjax(`${app.globalData.apiUrl}/Api/Food/get_food_content`,res => {
        const department = that.data.department.concat(res.data)
        // console.log(department)
        that.setData({
          department: department
        })
        // console.log(that.data.department)
      },{
          status: 1
      })
  },
  nameChange(e){
    const that = this;
    that.setData({
      uname: e.detail.value
    })
    
  },
  searchName() {
    const that = this;
    if (that.data.uname) {
      util.getAjax(`${app.globalData.apiUrl}/Api/Food/get_user_id`,res => {
        console.log(res.data)
        if(res.data) {
          that.data.user_id = res.data.id
          that.getOrderList()
        } else {
          util.showToast('系统不存在该员工！','none', 2000)
        }
      },{name: that.data.uname })
    } else {
      that.data.user_id = '';
      that.getOrderList()
    }
  },
  // 去详情
  toPages(e){
    var _this = this;
    var index = e.currentTarget.dataset.index;
    wx.navigateTo({
        url:_this.data.navList[index].link
    })
  },
  changeNav(e){
    var index = e.currentTarget.dataset.index;
    console.log(index)
    var _this = this;
    var countList = _this.data.countList;
    var type = countList[index].value
    console.log(type)
    _this.setData({
        currentNav:index,
        type: type
    })
    _this.getOrderList()
  },
// hasNew(){
//     var _this = this;
//     util.getAjax(`${app.globalData.apiUrl}/Api/Food/get_user`,res=>{
//         console.log(res.data.length)
//         var navList = _this.data.navList;
//         if(res.data.length!=0){
//             // console.log(2);
//             navList[1].warning = true
//             _this.setData({
//                 navList:navList
//             })
//         }else{
//             navList[1].warning = false
//             _this.setData({
//                 navList:navList
//             })
//         }
//     },{
//         status:4
//     })
// },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options);
      var type = options.type;
      console.log(type);
      var date = '';
      var dateAll = '';
    if(type == 0){
        var date = util.setTime(new Date());
        console.log(date);
    }
    if(type == 1){
        this.setData({
            isShow:false,
            content_id: options.content_id,
            user_id: options.user_id
        })
        var date = options.date
        console.log(date);
    }
    var week = util.setWeek(date);
    dateAll = date.split('/');
    dateAll = dateAll[0] + '年' + dateAll[1] + '月' + dateAll[2] + '日'
      this.setData({
          date:date,
          dateAll:dateAll,
          week:week
      })
    util.setNavBarTitle('订餐管理');
  },
  setDateType() {
    let timeArr = app.globalData.mainTime;
    let am = util.setDate('00:00', timeArr[1].food_time); 
    let pm = util.setDate(timeArr[1].food_time, '23:59');
    let dateType = '';
    let amOpm = '';
    if(am) {
      dateType = 1
      amOpm = '午餐'
    } else if(pm) {
      dateType = 2
      amOpm = '晚餐'
    }
    console.log('dateType', dateType)
    this.setData({
      dateType: dateType,
      amOpm: amOpm
    })
    this.getOrderList()
  },
    // count(date){
    //     var _this = this;
    //      util.getAjax(`${app.globalData.apiUrl}/Api/Food/get_day_food_order`,res=>{
    //       console.log('count', res);
    //       var countList = _this.data.countList;
    //       countList[0].count = res.data.one;
    //       countList[1].count = res.data.two;
    //       countList[2].count = res.data.zero;
    //       _this.setData({
    //           allData:res.data,
    //           countList:countList,
    //           userList:res.data.one_list
    //       })
    //   },{
    //       date:date
    //   })
    // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.hasNew();
    // this.count(this.data.date);
    this.getDepartment()
    // this.getOrderList()
    this.setDateType()
    console.log(app.globalData.mainTime)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //   this.hasNew();
      wx.showToast({
          title:'数据刷新',
          icon:'loading',
          duration:1000
      });
      console.log('pullDown');
    //   this.count(this.data.date);
      this.getOrderList()
      wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
